package com.company.nuwa.common.utils;

import com.google.common.base.Strings;

import java.security.SecureRandom;
import java.util.Random;

/**
 * <p>随机数</p >
 * @author shuaishuai.xiao
 * @version 1.0: RandomUtils.java
 * @date 2019/12/11 14:09
 */

public class RandomUtils {
    public static final String  SYMBOLS        = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String  SYMBOLS_NUMBER = "0123456789";

    private static final Random RANDOM         = new SecureRandom();

    /**
     * 随机串
     * @param length 长度
     * @param origin 源数据
     * @return
     */
    public static String generateNonce(int length, String origin) {
        if (length <= 0 || Strings.isNullOrEmpty(origin)) {
            return "";
        }
        char[] nonceChars = new char[length];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = origin.charAt(RANDOM.nextInt(origin.length()));
        }
        return new String(nonceChars);
    }

}
